<?php
namespace app\servergoods\logic;

use \app\manage\logic\BaseLogic;

// 商品分类属性微应用下的逻辑层
// dbXXXX : 不作为接口返回json数据，用于内部调用
class GoodsCategoryAttrs extends BaseLogic
{
	protected $name = 'goods_category_attrs';
    protected $config = 'db_config_goods';
    protected $cateAttrsModel = 'app\servergoods\model\GoodsCategoryAttrs';
    protected $searchWhere = ['name'=>'like','attr_type'=>'eq'];
    /*添加属性*/
    public function addAttr($data)
    {
    	$res = $this->model($this->cateAttrsModel)->save($data);
    	return auto_return($res);
    }
    /*编辑属性*/
    public function editAttr($data)
    {
        $_where['id'] = $data['id'];
    	$res = $this->model($this->cateAttrsModel)->save($data, $_where);
    	return auto_return($res);
    }
    /*获取某属性信息*/
    public function oneAttrInfo($data)
    {
        isset($data['id']) && $_where['id'] = $data['id'];
        isset($data['en_name']) && $_where['en_name'] = $data['en_name'];
        $_where['status'] = ['neq', -1];
        $res = $this->model($this->cateAttrsModel)
                    ->where($_where)
                    ->find();
        return auto_return($res);
    }
    /*获取属性列表*/
    public function attrsList($data)
    {
        

        $_where = $this->searchWhere($data,$this->searchWhere);
        $_where['status'] = ['neq',-1];

        $res = $this->model($this->cateAttrsModel)
                    ->where($_where)
                    ->order('id', 'desc')
                    ->paginate(null,false,['page'=>$data['page'],'list_rows'=>$data['listRows']])
                    ->toArray();

        return auto_return_page($res);
    }
    /*获取所有属相列表 无分页*/
    public function attrsAllList($data = [])
    {
        
        $_where = $this->searchWhere($data,$this->searchWhere);
        $_where['status'] = ['neq',-1];

        $res = $this->model($this->cateAttrsModel)
                    ->where($_where)
                    ->select();

        return auto_return($res);
    }
    //db获取属性列表 无分页 用于内部调用
    public function dbAttrsList($map = [], $field = true)
    {
        
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['en_name']) && $_where['en_name'] = $map['en_name'];
        $_where['status'] = ['neq',-1];
        return $this->db()
                    ->field($field)
                    ->where($_where)
                    ->select();

    }
    //删除一个或多个属性
    public function delAttrs($data)
    {

        $_where['id'] = ['in',$data['id']];
        $_where['status'] = ['neq',-1];
        $saveData['status'] = -1;
        $res = $this->db()
                    ->where($_where)
                    ->update($saveData);

        return auto_return($res);
    }

    /*根据en_name获取多个属性详情*/
    public function getSomeAttrs($data): array 
    {
        $_where['en_name'] = ['in', $data['en_name']];
        $_where['status'] = ['neq', -1];
        $res = $this->model($this->cateAttrsModel)
                    ->order('`order`,id desc')
                    ->where($_where)
                    ->select();

        return auto_return($res);
    }

}